﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using _430GroupProject;

namespace WebClient.PharosClient
{
    public partial class Alerts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }

            //Load Possible Alerts
            List<Alert> types = new List<Alert>();
            foreach(Alert alType in Enum.GetValues(typeof(Alert)))
            {
                types.Add(alType);
            }

            SelectedAlert.DataSource = types;
            SelectedAlert.DataBind();
            if (!IsPostBack)
            {
                SelectedAlert.SelectedValue = types[0].ToString();
            }
        }


        protected void UserSelect_Load(object sender, EventArgs e)
        {
            User user = (User)Session["user"];
            List<string> userList = new List<string>();
            if (user.Permissions.Contains(Permission.Admin))
            {
                userList = Master.Proxy.getAllUserNames();
            }
            else
            {

                WorkGroups manages = Master.Proxy.SupervisorFor(user.NetID);
                foreach(WorkGroup wg in manages.Groups)
                {
                    foreach (User u in wg.Employees)
                    {
                        userList.Add(u.NetID );
                    }
                } 
            }

            UserSelect.DataSource = userList;
            UserSelect.DataBind();
            if (!IsPostBack)
            {
                UserSelect.SelectedValue = userList[0];
            }
            showAlerts();
        }

        private void showAlerts()
        {
            User editing = Master.Proxy.getUser(UserSelect.SelectedValue);
            if (editing.Alerts == null)
            {
                editing.Alerts = new List<Alert>();
            }
            foreach (Alert al in editing.Alerts)
            {
                Label toAdd = new Label();
                toAdd.Text = al.ToString();
                CurrentAlertsContainer.Controls.Add(toAdd);
            }
        }

        protected void AddAlertButton_Click(object sender, EventArgs e)
        {
           // SelectedAlert.SelectedValue
            User editing = Master.Proxy.getUser(UserSelect.SelectedValue);
            if (editing.Alerts == null)
            {
                editing.Alerts = new List<Alert>();
            }
            if (Enum.IsDefined(typeof(Alert), SelectedAlert.SelectedValue) && !editing.Alerts.Contains((Alert)Enum.Parse(typeof(Alert), SelectedAlert.SelectedValue, true))) //Add Alert only if it is not already present
            {
                editing.Alerts.Add((Alert)Enum.Parse(typeof(Alert), SelectedAlert.SelectedValue, true));
            }

            Master.Proxy.updateUser(editing);
            MainPanel.Update();
        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }
    }
}